package com.jf.cloud.service.sysmanager.common.taglib.html;


import java.io.IOException;
import java.util.Enumeration;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.jf.cloud.service.sysmanager.common.util.RandomUtil;

/**
 * 防刷新 form 标签
 * @author jspp@qq.com
 *
 */
public class FormTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String action;
    private String method;
    private String name;
    private String cssClass;
    private String enctype;
    private String id;
    private String target;
    private String onsubmit;
    private String relayQc; // 是否转接查询条件
   /**
    * 执行tag-lib输出
    * @throws JspException
    * @return int
    */
   public int doStartTag() throws JspException {
	try {
		pageContext.getOut().write(this.getHtmlString());
	} catch (IOException e) {
		e.printStackTrace();
	}
       return EVAL_BODY_INCLUDE;
    }

   public int doEndTag() throws JspException {
	 try {
		pageContext.getOut().write("</form>");
	} catch (IOException e) {
		e.printStackTrace();
	}
      return EVAL_BODY_INCLUDE;
    }

   /**
    * 生成页面信息
    * @return String
    */

   private String getHtmlString() {
		// 获得一个随机数，防止页面刷新
		Random random = new Random();
		String rand = String.valueOf(random.nextInt(10000000));
		String randAttribute = RandomUtil.getRandomLeterAndNumber(10);
		((HttpServletRequest) pageContext.getRequest()).getSession(false).setAttribute(randAttribute, rand);
		StringBuffer str = new StringBuffer();
		// 组成页面元素
		str.append("<form action=\"" + getAction() + "\" method=\"" + getMethod() + "\" ");

		if (getName() != null && getName().length() > 0) {
			str.append(" name=\"" + getName() + "\"");
		}
		if (getId() != null && getId().length() > 0) {
			str.append(" id=\"" + getId() + "\"");
		}
		if (getCssClass() != null && getCssClass().length() > 0) {
			str.append(" class=\"" + getCssClass() + "\"");
		}
		if (getTarget() != null && getTarget().length() > 0) {
			str.append(" target=\"" + getTarget() + "\" ");
		}
		if (getOnsubmit() != null && getOnsubmit().length() > 0) {
			str.append(" onsubmit=\"" + getOnsubmit() + "\" ");
		}
		if (getEnctype() != null && getEnctype().length() > 0) {
			str.append(" enctype=\"" + getEnctype() + "\" ");
		}
		str.append(" >" + "\n");
		str.append("<input type=\"hidden\" name=\"rand_n_\" value=\"" + rand + "\">" + "\n");
		str.append("<input type=\"hidden\" name=\"randAttribute_v_\" value=\"" + randAttribute + "\">" + "\n");

		// 转接查询条件
		if (relayQc != null && relayQc.equals("true")) {
			Enumeration<?> paramNames = pageContext.getRequest().getParameterNames();
			while (paramNames.hasMoreElements()) {
				String paraName = (String) paramNames.nextElement();
				if (paraName != null && paraName.startsWith("wm[")) {
					String paramValue = pageContext.getRequest().getParameter(paraName);
					str.append("<input type=\"hidden\" name=\"" + paraName + "\" value=\"" + paramValue + "\">" + "\n");
				}
			}
		}
		return str.toString();
	}

    public String getAction() {
        return action;
    }

    public String getEnctype() {
        return enctype;
    }

    public String getMethod() {
        return method;
    }

    public String getName() {
        return name;
    }
    public void setAction(String action) {
        this.action = action;
    }
    public void setMethod(String method) {
        this.method = method;
    }
    public void setEnctype(String enctype) {
        this.enctype = enctype;
    }
    public void setName(String name) {
        this.name = name;
    }

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getOnsubmit() {
		return onsubmit;
	}

	public void setOnsubmit(String onsubmit) {
		this.onsubmit = onsubmit;
	}
   
	public String getRelayQc() {
		return relayQc;
	}

	public void setRelayQc(String relayQc) {
		this.relayQc = relayQc;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}
}
